// store/index.js
import { createStore } from 'vuex';
import axios from 'axios';
import { ElMessage } from 'element-plus';
import editmodemenu from '../../script/manage/menuaction/childmenu/editmodemenu';

export default createStore({
  state() {
    return {
      uid: '',
      name: '',
      token: '',
      activeCanvasTabName: 'defaultCanvas',
      curentFile: { uuid: '', name: '', parent: '', projectid: '', userid: '' },
      workMode: 'autocad',
      symbolDefData: { unitArr: [], symbolArr: [],patterArr:[]},
      cacheTemplateList: [],
      onShowToolBox:null,
      cacheMyFileList:[],
      haveLoadCategoryList:[],
      lazyOpenLocalFileInfo:null
    };
  },
  mutations: {
    updateSymbolData(state, symRet) {
      symRet.unitArr.forEach(t => {
        state.symbolDefData.unitArr.push(t);
      });

      symRet.symbolArr.forEach(t => {
        state.symbolDefData.symbolArr.push(t);
      });
  
      let list = symRet.categoryList;
      list.forEach(t => {
        if (state.haveLoadCategoryList.indexOf(t) == -1){
          state.haveLoadCategoryList.push(t);
        }
      });
    },

    updatePatternData(state, patterArr) {
      state.symbolDefData.patterArr = patterArr;
    },

    updateUser(state, user) {
      let preUserID = state.uid;
      state.uid = user.uid;
      state.name = user.name;
      state.token = user.token;

      if (preUserID.length > 0 && preUserID != user.uid) {
        state.curentFile.uuid = "";
        state.curentFile.name = "";
        state.curentFile.parent = "";
        state.curentFile.projectid = "";
        state.curentFile.userid = "";
        state.curentFile.fileMode = state.workMode;
      }
    },

    switchLazyFile(state, fileInfo) {
      console.log("switchLazyFile");
      state.curentFile.uuid = fileInfo.uuid;
      state.curentFile.name = fileInfo.name;
      state.curentFile.parent = fileInfo.parent;
      state.curentFile.projectid = fileInfo.projectid;
      state.curentFile.fileMode = fileInfo.fileMode;
      if (!fileInfo.userid) {
        state.curentFile.userid = state.uid;
      }
      else {
        state.curentFile.userid = fileInfo.userid;
      }
    },

    switchFile(state, fileInfo) {
      console.log("switchFile");
      state.curentFile.uuid = fileInfo.uuid;
      state.curentFile.name = fileInfo.name;
      state.curentFile.parent = fileInfo.parent;
      state.curentFile.projectid = fileInfo.projectid;
      state.curentFile.fileMode = fileInfo.fileMode;
      if (!fileInfo.userid) {
        state.curentFile.userid = state.uid;
      }
      else {
        state.curentFile.userid = fileInfo.userid;
      }
      let list = window.canvasList.value;
      list.forEach(ele => {
        if (ele.name == state.activeCanvasTabName) {
          ele.text = fileInfo.name;
          //文件绑定到当前激活的画布上
          ele.refFile.uuid = state.curentFile.uuid;
          ele.refFile.name = state.curentFile.name;
          ele.refFile.parent = state.curentFile.parent;
          ele.refFile.projectid = state.curentFile.projectid;
          ele.refFile.userid = state.curentFile.userid;
          ele.refFile.fileMode = state.curentFile.fileMode;
          let editMenu = new editmodemenu.EditModeMenu(ele.mapCtrlPtr.mainctl, ele.refFile.fileMode);
          editMenu.act2Click(true);
        }
      });
    },

    lazyOpenLocalFile(state,localFileInfo){
      state.lazyOpenLocalFileInfo = localFileInfo;
    },
    releaseLazyOpenLocalFile(state){
      state.lazyOpenLocalFileInfo = null;
    },
    switchActiveCanvasTab(state, canvasTabName) {
      console.log("switchActiveCanvasTab");
      state.activeCanvasTabName = canvasTabName;

      let list = window.canvasList.value;
      list.forEach(ele => {
        if (ele.name == state.activeCanvasTabName) {
          state.curentFile.uuid = ele.refFile.uuid;
          state.curentFile.name = ele.refFile.name;
          state.curentFile.parent = ele.refFile.parent;
          state.curentFile.projectid = ele.refFile.projectid;
          state.curentFile.userid = ele.refFile.userid;

          //把当前激活的画布的绑定的文件设置当前激活文件
        }
      });
    },

    // 退出操作：清空jwt即可
    // 同步操作，可直接写到mutation
    logout(state) {
      state.uid = "";
      state.name = "";
      state.token = "";

      state.curentFile.uuid = "";
      state.curentFile.name = "";
      state.curentFile.parent = "";
      state.curentFile.projectid = "";
      state.curentFile.userid = "";
      if (window.mainctl) {
        window.mainctl.clearData();
      }
    },

    releaseLastOpenFile(state){
      state.curentFile.uuid = "";
      state.curentFile.name = "";
      state.curentFile.parent = "";
      state.curentFile.projectid = "";
      state.curentFile.userid = "";
      state.curentFile.fileMode = "";
    },

    switchWorkMode(state, param) {
      state.workMode = param.mode;
      let editMenu = new editmodemenu.EditModeMenu(param.map, param.mode);
      editMenu.act2Click(false);
    },
    updateTemplateFile(state, list) {
      state.cacheTemplateList = list;
    },

    updateMyFileList(state, list) {
      state.cacheMyFileList = list;
    },

    switchOnShowToolBox(state,showFunc){
      state.onShowToolBox = showFunc;
    }
  },
  actions: {
    // context传API，data 自定义传
    login: (context, data) => {
      console.log("store login******************");
      var url = "api/user/login";
      axios.post(url, data).then(res => {
        console.log(res.data);
        let rstObj = res.data;
        if (rstObj.ret == "success") {
          let user = {
            uid: rstObj.data.uid,
            name: rstObj.data.name,
            token: rstObj.data.token
          };
          context.commit("updateUser", user);
          ElMessage('登录成功');
        }
        else {
          ElMessage('密码错误。登录失败');
        }
      }).catch(function (error) {
        console.log(error);
        ElMessage('登录失败');
      })
    }
  },
  getters: {

  }
});
